<template>
  <div class="register">
    <h2 class="title">Last Order</h2>
    <img class="logo" src="@/images/logo.png" alt />
    <div class="cell-group">
      <span>用户名</span>
      <input placeholder="请输入用户名" type="text" v-model="userName" />
    </div>
    <div class="cell-group">
      <span>密码</span>
      <input placeholder="请输入密码" type="password" v-model="password" />
    </div>
    <div class="sub">
      <router-link to="/register" tag="p">还没有账号，去注册</router-link>
      <van-button round type="default" size="small" @click="tap()">登录</van-button>
    </div>

    <span class="share">——&nbsp;第三方登入&nbsp;——</span>
    <div class="bshare-custom">
      <a title="分享到QQ好友" class="icon bshare-qqim" href="javascript:void(0);">
        <img src="../images/QQ.png" alt class="icon bshare-qqim" href="javascript:void(0);" />
      </a>
      <a title="分享到新浪微博" class="icon bshare-sinaminiblog">
        <img
          src="../images/xinlangweibo.png"
          alt
          class="icon bshare-sinaminiblog"
          href="javascript:void(0);"
        />
      </a>
      <a title="分享到微信" class="icon bshare-weixin">
        <img src="../images/weixin.png" alt class="icon bshare-weixin" href="javascript:void(0);" />
      </a>
    </div>
  </div>
</template>

<script>
import * as api from "@/api/userInfo";
export default {
  name: "Login",
  data() {
    return {
      userName: "",
      password: ""
    };
  },
  methods: {
    tap() {
      let params = {
        userName: this.userName,
        password: this.password
      };
      api.login(params).then(data => {
        if (data.data.code == "success") {
          this.$dialog
            .alert({
              message: "登录成功"
            })
            .then(() => {
              console.log(data.data);
              localStorage.setItem("token", data.data.token);
              api.getUserInfo().then(data => {
                console.log(data.data.nickName);
                this.$store.commit("loginU", data.data.nickName);

                this.$router.push("./index");
              });
            });
        } else {
          this.$dialog
            .alert({
              title: "登录失败",
              message: "用户名和密码错误"
            })
            .then(() => {
              (this.userName = ""), (this.password = "");
            });
        }
      });
    }
  },
  mounted() {
    console.log(this.$store.state.nickName);
  }
};
</script>

<style scoped>
.logo {
  margin: 20px auto;
  width: 40%;
}
.register {
  height: 100vh;
  background: url(../images/bg_1.png);
  background-size: 100%;
  overflow: hidden;
  padding: 0 40px;
  text-align: center;
}
.title {
  font-size: 24px;
  font-weight: 500;
  color: #fff;
  font-family: "微软雅黑";
  text-align: center;
  margin-top: 20px;
}
.cell-group {
  margin-bottom: 20px;
  display: flex;
}
.cell-group:nth-of-type(3) {
  margin: 0;
}
.cell-group span {
  width: 80px;
  display: block;
  color: #fff;
  font-size: 16px;
  text-align: left;
}
input {
  flex: 1;
  background: none;
  border: 0;
  color: #fff;
  font-size: 14px;
  line-height: 28px;
  padding-left: 10px;
  border-bottom: 1px solid #fff;
}
input::-webkit-input-placeholder {
  color: #eee;
}
.sub {
  text-align: center;
}
.sub .van-button {
  margin: 36px 0 114px;
  width: 240px;
  height: 40px;
  font-size: 20px;
  color: #74ba42;
}
.sub p {
  color: rgb(240, 240, 240);
  font-size: 12px;
  line-height: 36px;
  text-align: right;
  margin-bottom: 10px;
}
.share {
  display: block;
  width: 100%;
  text-align: center;
  margin-top: 30px;
  line-height: 32px;
  font-size: 12px;
  color: #333;
}
.bshare-custom {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}
.bshare-custom a {
  display: block;
  width: 40px;
  height: 40px;
  background-size: 60%;
}
.bshare-custom .icon {
  background: none;
  margin: 0;
  padding: 0;
}
.bshare-custom .icon img {
  width: 39px;
}
</style>